import { Component, OnInit } from '@angular/core';
import { SimpleTableColumn } from '@delon/abc';
import { NzMessageService } from 'ng-zorro-antd';
import { UserService } from './user.service'
import { SysUser } from './user';

@Component({
    selector: 'app-user-list',
    templateUrl: './user-list.component.html',
    providers: [UserService]
})

export class UserListComponent implements OnInit {

    q: any = {
        pi: 1,
        ps: 10,
        sorter: '',
        status: -1,
        statusList: []
    };
    user: SysUser = new SysUser();
    _dataSet: SysUser[] = [];
    //data: any[] = [];
    selectedRows: any[] = [];
    _loading = false;
    expandForm = false;
    modalVisible = false;
    //params = { a: 1, _allow_anonymous: true };
    // mock
    total = 20;
    columns: SimpleTableColumn[] = [
        { title: '编号', index: 'id' },
        { title: '头像', type: 'img', width: '50px', index: 'avatar' },
        { title: '用户名', index: 'account' },
        { title: '昵称', index: 'name' },
        { title: '邮箱', index: 'email' },
        { title: '电话', index: 'phone' },
        { title: '注册时间', type: 'date', index: 'createDate' },
        {
            title: '操作',
            buttons: [
                {
                    text: '删除', type: 'del', click: (record: any) => {
                        this.userService.deleteUser(record.id).then(response => {
                            if (response.code === 0) {
                                this.msg.success(`${record.account} has deleted!`);
                                this.getData();
                            }
                            else {
                                this.msg.success(response.message);
                            }
                        });
                    }
                }
            ]
        }
    ];

    constructor(private msg: NzMessageService, private userService: UserService) { }

    ngOnInit() {
        this.getData();
    }

    add() {
        this.modalVisible = true;
    }

    save() {
        this._loading = true;
        this.userService.saveUser(this.user)
            .then(response => {
                this.getData();
                this.modalVisible = false;
                this._loading = false;
            });

    }

    getData() {
        this.userService.getUserList(this.q)
            .then(response => {
                this._dataSet = response.data;
            });
    }

    del(record: any) {
        this.userService.deleteUser(record.id).then(response => {
            if (response.code === 0) {
                this.msg.success(`${record.account} has deleted!`);
                this.getData();
            }
            else {
                this.msg.success(response.message);
            }
        });
    }
}